package data;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Struct;
import java.util.ArrayList;
import java.util.Observable;
import java.util.Observer;

import javax.swing.JButton;
import javax.swing.JTable;
import javax.swing.border.EmptyBorder;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.AbstractTableModel;

import database.ConnexionDB;

public class ModeleDynamiqueObjetIndice extends AbstractTableModel implements Observer, TableModelListener {
	private Object[][] donnees;
	private final String[] entetes = {"Nom", "Type","Chemin"};
	private ArrayList<Indice> listIndice = new ArrayList<Indice>();
	private static ConnexionDB DB;
	//private static ConnexionDB DB = new ConnexionDB();
	
	Struct Compte;
	{
	  int   numero;
	  float solde;
	} ;
	
	
	public ModeleDynamiqueObjetIndice (ConnexionDB BD)
	{
		super();
		DB = BD;
		this.addTableModelListener(this);;
	}
	
	public ConnexionDB getDB() {
		return DB;
	}
	public ArrayList<Indice> getListIndice() {
		return listIndice;
	}
	public int getRowCount() {
        return listIndice.size();
    }

    public int getColumnCount() {
        return entetes.length;
    }

    public String getColumnName(int columnIndex) {
        return entetes[columnIndex];
    }

    public Object getValueAt(int rowIndex, int columnIndex) {
    	if(rowIndex > listIndice.size()){
    		return null;
    	}
    	else {
    		switch(columnIndex){
            case 0:
                return listIndice.get(rowIndex).getLabelIndice();
            case 1:
                return listIndice.get(rowIndex).getTypeIndice();
            case 2:
                return listIndice.get(rowIndex).getCheminIndice();
         
            default:
                return null; 
    		}
    	}
    }
    
    /*public boolean isCellEditable(int row, int column) {
        return true;
      }*/
    
    public void removeRow(int row) {
		listIndice.remove(row);
		fireTableStructureChanged();
	}
    
    public void insertRow(int row, Indice ind) {
    	listIndice.add(row, ind);
		fireTableStructureChanged();
	}
    
    public void addRow(Utilisateur u1) {
		addRow(u1);
		fireTableStructureChanged();
	}

	public void addIndice(Indice indice) {
		listIndice.add(indice);
        //fireTableRowsInserted(listIndice.size() -1, listIndice.size() -1);
    }
	
	public void clear(){
		listIndice.clear();
		fireTableStructureChanged();
	}
	
	
	public void insererIndice(final Indice I1) throws SQLException
	{
		
		final  Indice i = I1;
		new Thread(new Runnable() {
			@Override
			public void run() {
				//Auto-generated method stub
				try {
					
					getDB().insertLigneIndice(i.getLabelIndice(), i.getTypeIndice(),i.getCheminIndice(),i.getDescription_indice(),i.getCoutIndice());
				} catch (SQLException e) {
					System.out.println("lol test");
					//  Auto-generated catch block
					e.printStackTrace();
				}
			}
		}).start();
	}
	
	public ArrayList<Indice> getListIndices() {
		return listIndice;
	}
	public void setListIndice(ArrayList<Indice> listIndice) {
		this.listIndice = listIndice;
	}
	
	public void deleteIndice(final int id) throws SQLException
	{	
		new Thread(new Runnable() {
			@Override
			public void run() {
				// Auto-generated method stub
				try {
						getDB().deleteLigneIndice(id);
				} catch (SQLException e) {
					//  Auto-generated catch block
					e.printStackTrace();
				}
			}
		}).start();
	}
	
	
	
	public void update(Observable arg0, Object arg1){
		// DataBase changed`
		
		ConnexionDB DB = (ConnexionDB) arg0;
		String requeteSelectIndice = "SELECT * FROM indices";
      	 
        ResultSet resultat;

        clear();
        listIndice.clear();
			
        try {
			resultat = DB.executerRequete(requeteSelectIndice);
			while(resultat.next()){
	   			   Indice ind = new Indice();
	               //System.out.println(resultat.getString("id_utilisateur") + "\t\t" + resultat.getString("pseudo") + "\t\t" + resultat.getString("password")+ "\t\t" + resultat.getString("admin")+ "\t\t" + resultat.getString("chrono"));
	               ind.setIdIndice(resultat.getInt("id_indice"));
	               ind.setLabelIndice(resultat.getString("label_indice"));
	               ind.setTypeIndice(resultat.getString("type_indice"));
	               ind.setCheminIndice(resultat.getString("chemin_indice"));
	          
	               
	               addIndice(ind);
	           }
		} catch (SQLException e) {
			//  Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	public void tableChanged(TableModelEvent arg0) {
		//  Auto-generated method stub
		
	}

	public Indice getInfosIndice(int id_indice) {
		// Auto-generated method stub
	//	ConnexionDB DB = (ConnexionDB) arg0;

      	 
        ResultSet resultat;
        Indice ind1 = new Indice();
       // clear();
        //listIndice.clear();
        
        ind1 = DB.getinfosIndice(id_indice);
	
	
	
		return ind1;
	}

	public void modifier_Indice(Indice ind1) {
		//Auto-generated method stub
		
		DB.modifier_indice(ind1);
		
	}


	public  ArrayList<Utilisateur> getJoueurIndice(int id_indice) {
		//  Auto-generated method stub
		return DB.getJoueur_indice(id_indice);
	}

	public String getPseudoUser(int idUtilisateur) {
	
		return DB.getPseudoUser(idUtilisateur);


	}

	public ArrayList<Utilisateur> getUserList() {
		// Auto-generated method stub
	
		return DB.getUser();
	}

	public ArrayList<Indice> getIndiceList() {
		//  Auto-generated method stub
		return DB.getIndiceList();
	}

	public int getIdIndice(String selectedItem) {
		// Auto-generated method stub
		return DB.getIdIndice(selectedItem);
	}

	public int getIdJoueur(String selectedItem) {
		//  Auto-generated method stub
		return DB.getIdJoueur(selectedItem);
	}

	public static void envoyerIndice(int id_indice, int id_joueur) {


		DB.envoyerIndice(id_indice,id_joueur);
		
	}

	public String getChemin(Integer integer) {
		//  Auto-generated method stub
		return DB.getCheminIndice(integer);
	}

	public void enleverIndiceJoueur(int id_indice, int id_joueur) {

		DB.deleteIndiceJoueur(id_indice,id_joueur);
		
		
	}
	
	

}
